package com.ruoyi.business.order.service.impl;


import com.ruoyi.business.base.JoBaseServiceImpl;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.order.domain.JbOrderCommodity;
import com.ruoyi.business.order.mapper.JbOrderCommodityMapper;
import com.ruoyi.business.order.service.JbOrderCommodityService;
import org.springframework.stereotype.Service;

import java.util.StringJoiner;

/**
 * 订单商品Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Service
public class JbOrderCommodityServiceImpl
        extends JoBaseServiceImpl<JbOrderCommodityMapper, JbOrderCommodity>
        implements JbOrderCommodityService
{

    @Override
    public Integer getOrderCommodityCount(PageParam param, JbOrderCommodity jbOrderCommodity)
    {
        // 暂时先不判断，那个状态是订单完成的有效订单
        StringJoiner sql = new StringJoiner("").add("""
                SELECT
                	count( DISTINCT joc.commodity_id ) as commodity_id
                FROM
                	jb_order_commodity joc
                	INNER JOIN jb_order jo ON joc.order_id = jo.id\s
                WHERE
                	jo.del_flag = 0\s
                	AND joc.del_flag = 0\s
                """);
        sql.add(joSqlUtil.sqlIf(String.format(" AND jo.create_time >= '%s'", param.getStartTimeBuilder())
                , param.getStartTimeBuilder()));
        sql.add(joSqlUtil.sqlIf(String.format(" AND jo.create_time <= '%s'", param.getEndTimeBuilder())
                , param.getEndTimeBuilder()));
        sql.add(joSqlUtil.sqlIf(String.format(" AND jo.create_by=%s", jbOrderCommodity.getCreateBy())
                , jbOrderCommodity.getCreateBy()));
        return baseMapper.findJsonCount(sql.toString());
    }
}
